package com.wfk.service.impl;

import com.wfk.alipay.AliSmsUtil;
import com.wfk.entity.BasicsSmsInfo;
import com.wfk.mapper.BasicsSmsInfoMapper;
import com.wfk.service.IBasicsSmsInfoService;
import com.wfk.utils.ResponseData;
import com.wfk.utils.ValidateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2019-01-07
 */
@Service
public class BasicsSmsInfoServiceImpl extends ServiceImpl<BasicsSmsInfoMapper, BasicsSmsInfo> implements IBasicsSmsInfoService {
	//验证码过期时间
		private static final int OUTTIME=10;
		@Override
		public ResponseData sendSMS(String mobile, String sign, String code) {
			try {
				if(!ValidateUtil.empty(mobile)){
					return ResponseData.baseServiceError("短信发送失败,手机号码为空");
				}
				String param = getSix();
				
				//先保存即将发送的短信信息
				BasicsSmsInfo basicsSmsInfo=new BasicsSmsInfo();
				basicsSmsInfo.setPhone(mobile);
				basicsSmsInfo.setSignname(sign);
				basicsSmsInfo.setContent(code);
				basicsSmsInfo.setType(1);
				basicsSmsInfo.setParams(param);
				
				super.save(basicsSmsInfo);
				String result = AliSmsUtil.SendSMS(mobile,sign,code,param);
				if(result.length() == 0){
					//短信发送成功修改记录信息
					basicsSmsInfo.setStatus(0);
					super.updateById(basicsSmsInfo);
					return ResponseData.ok("短信发送成功");
				}else{
					//发送失败 修改信息
					basicsSmsInfo.setStatus(-1);
					basicsSmsInfo.setErrorcode(result);
					super.updateById(basicsSmsInfo);
					return ResponseData.baseServiceError("短信发送失败");
				}
			} catch (Exception e) {
				e.printStackTrace();
				return ResponseData.baseServiceError("短信发送异常，请联系客服");
			}
		}

		@Override
		public ResponseData validSMS(String mobile, String validCode) {
			return validSMS(mobile, validCode,true);
		}

		@Override
		public ResponseData validSMS(String mobile, String validCode, boolean fla) {
			QueryWrapper<BasicsSmsInfo> queryWrapper = new QueryWrapper<BasicsSmsInfo>();
			queryWrapper.eq("type",1);
			queryWrapper.eq("status",0);
			queryWrapper.eq("phone",mobile);
			queryWrapper.eq("params",validCode);
			queryWrapper.orderByDesc("createtime");
			try {
				BasicsSmsInfo bInfo = super.getOne(queryWrapper);
				if (bInfo != null) {
					long validTime = bInfo.getCreatetime().getTime() + (TimeUnit.MILLISECONDS.convert(OUTTIME, TimeUnit.MINUTES));
					if (bInfo.getIsused() == 0) {
						return ResponseData.baseServiceError("验证码已被使用");
					}
					if (validTime < System.currentTimeMillis()) {
						return ResponseData.baseServiceError("验证码已过期");
					}
					if (fla) {
						// 修改验证码状态
						bInfo.setIsused(0);
						super.updateById(bInfo);
					}
				} else {
					return ResponseData.baseServiceError("验证码不正确");
				}
			} catch (Exception e) {
				return ResponseData.baseServiceError("查询验证信息失败");
			}
			return ResponseData.ok();
		}
		/** 
	     * 产生随机的六位数 
	     * @return 
	     */  
		private  String getSix(){  
	        Random rad=new Random();  
	        String result  = "";  
	        for (int i = 0; i < 6; i++) {
	        	result += rad.nextInt(10);
			}
	        return result;  
	    }
}
